package com.mosai.alignpro.adapter;

import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.mosai.alignpro.BaseActivity;
import com.mosai.alignpro.EditDescribeActivity;
import com.mosai.alignpro.R;
import com.mosai.alignpro.entity.ImageInfo;
import com.mosai.alignpro.util.DateUtils;
import com.mosai.alignpro.util.IntentUtils;
import com.mosai.alignpro.util.TimeUtils;
import com.mosai.alignpro.util.Utils;
import com.mosai.alignpro.view.DiagnosisView;

import java.util.List;

/**
 * Created by Rays on 2020/3/24.
 */
public final class DiagnosisListByPatientVH extends BaseViewHolder {
    private final DiagnosisView diagnosisView;
    private final TextView tvRightName;
    private final TextView tvImgDate;
    private final TextView tvHint;
    private final TextView tvConditionStatusTitle;
    private final TextView tvConditionStatusDesc;
    private final TextView tvDiagnosisResult;
    private final TextView tvStatusAudited;
    private final TextView tvImageType;
    private final TextView tvDiagnosisConclusion;
    private final TextView tvScoliosisType;
    private final View layoutScoliosisType;
    private final View layoutResult;
    private final View layoutConditionStatusTitle;
    private final View layoutConditionStatus;
    private final View diagnosisColorPreview;
    private final ImageView ivRightAvatar;

    private final BaseActivity baseActivity;
    private final RequestOptions doctorOptions;

    DiagnosisListByPatientVH(View itemView, BaseActivity baseActivity, RequestOptions doctorOptions) {
        super(itemView);
        this.baseActivity = baseActivity;
        this.doctorOptions = doctorOptions;
        diagnosisView = itemView.findViewById(R.id.diagnosisView);
        ivRightAvatar = itemView.findViewById(R.id.ivRightAvatar);
        tvRightName = itemView.findViewById(R.id.tvRightName);
        tvImgDate = itemView.findViewById(R.id.tvImgDate);
        tvConditionStatusTitle = itemView.findViewById(R.id.tvConditionStatusTitle);
        tvConditionStatusDesc = itemView.findViewById(R.id.tvConditionStatusDesc);
        tvDiagnosisResult = itemView.findViewById(R.id.tvDiagnosisResult);
        tvStatusAudited = itemView.findViewById(R.id.tvStatusAudited);
        tvHint = itemView.findViewById(R.id.tvHint);
        diagnosisColorPreview = itemView.findViewById(R.id.diagnosisColorPreview);
        layoutResult = itemView.findViewById(R.id.layoutResult);
        layoutConditionStatusTitle = itemView.findViewById(R.id.layoutConditionStatusTitle);
        layoutConditionStatus = itemView.findViewById(R.id.layoutConditionStatus);
        tvImageType = itemView.findViewById(R.id.tvImageType);
        layoutScoliosisType = itemView.findViewById(R.id.layoutScoliosisType);
        tvScoliosisType = itemView.findViewById(R.id.tvScoliosisType);
        tvDiagnosisConclusion = itemView.findViewById(R.id.tvDiagnosisConclusion);
    }

    @Override
    public void onBindViewHolder(ImageInfo item, List<Object> payloads) {
        for (Object payload : payloads) {
            if (DiagnosisListAdapter.PAYLOAD_DESCRIBE.equals(payload.toString())) {
                setDescribe(item);
            }
        }
    }

    @Override
    public void onBindViewHolder(ImageInfo item, int position) {
        diagnosisColorPreview.setVisibility(View.GONE);
        boolean showAngle = item.isAudited(); // 是否显示角度信息，目前仅审核后的显示
        boolean hasAngleInfo = showAngle && Utils.hasAngleInfo(item);
        layoutConditionStatus.setVisibility(hasAngleInfo ? View.VISIBLE : View.GONE);
        if (item.isShowCondition()) {
            setConditionStatusText(item);
            layoutResult.setVisibility(View.GONE);
            layoutConditionStatusTitle.setVisibility(View.VISIBLE);
        } else {
            layoutConditionStatusTitle.setVisibility(View.GONE);
            layoutResult.setVisibility(hasAngleInfo ? View.VISIBLE : View.GONE);
            tvDiagnosisResult.setText(item.getNewAngleText());
        }
        setDescribe(item);
        Utils.setImageTypeText(tvImageType, item);
        layoutScoliosisType.setVisibility(Utils.setScoliosisTypeText(tvScoliosisType, item) ? View.VISIBLE : View.GONE);
        tvDiagnosisConclusion.setText(item.isAudited() ? R.string.diagnosis_conclusion : R.string.diagnosis_conclusion_ai);

        tvHint.setVisibility(item.isAudited() ? View.GONE : View.VISIBLE);
        if (item.getStatus() == ImageInfo.STATUS_NO_DATA) {
            ivRightAvatar.setVisibility(View.GONE);
            tvRightName.setVisibility(View.GONE);
            tvImgDate.setVisibility(View.GONE);
        } else {
            ivRightAvatar.setVisibility(View.VISIBLE);
            tvRightName.setVisibility(View.VISIBLE);
            tvImgDate.setVisibility(View.VISIBLE);

            if (item.isAudited()) {
                if (item.getDoctorInfo() != null) {
                    Glide.with(baseActivity)
                            .load(item.getDoctorInfo().getAvatar())
                            .placeholder(R.drawable.default_avatar)
                            .error(R.drawable.default_avatar)
                            .apply(doctorOptions)
                            .into(ivRightAvatar);
                    tvRightName.setText(item.getDoctorInfo().getName());
                }

                if (item.getLastOriginResult() != null) {
                    tvImgDate.setText(TimeUtils.getTimeText(baseActivity, item.getLastOriginResult().getCreationDate()));
                } else {
                    tvImgDate.setText(TimeUtils.getTimeText(baseActivity, item.getCreationDate()));
                }
            } else {
                Glide.with(baseActivity).clear(ivRightAvatar);
                Glide.with(baseActivity)
                        .load(R.drawable.avatar_ai)
                        .apply(doctorOptions)
                        .into(ivRightAvatar);
                tvRightName.setText(R.string.ai_name);

                tvImgDate.setText(TimeUtils.getTimeText(baseActivity, DateUtils.parseUTC(item.getAiDiagnosisDate())));
            }
        }

        tvStatusAudited.setVisibility(item.isAudited() ? View.VISIBLE : View.GONE);

        diagnosisView.setDisableDrawAngle(!showAngle);
        diagnosisView.setHasEditPoint(false);
        diagnosisView.load(item);
        diagnosisView.setTag(diagnosisView.getId(), position);
        if (item.getImageType() == ImageInfo.IMAGE_TYPE_BACK) {
            diagnosisView.setOnAngleListener(null);
            layoutConditionStatus.setVisibility(View.VISIBLE);
            if (TextUtils.isEmpty(item.getDescribe()) && (TextUtils.isEmpty(item.getNewDescribe()) || item.isNotEditDescribe())) {
                item.setNewDescribe(Utils.getDescribe(item, baseActivity));
            }
            setDescribe(item);
        } else {
            diagnosisView.setOnAngleListener((diagnosisView, angles, circlePoint) -> {
                boolean hasAngle = !angles.isEmpty();
                boolean hasData = hasAngle || circlePoint != null;
                diagnosisColorPreview.setVisibility(hasData ? View.VISIBLE : View.GONE);
                layoutConditionStatus.setVisibility(hasAngle ? View.VISIBLE : View.GONE);
                item.setAngles(angles);
                item.setCirclePoint(circlePoint);
                if (item.isShowCondition()) {
                    layoutResult.setVisibility(View.GONE);
                    if (item.getNewConditionStatus() == ImageInfo.CONDITION_STATUS_INIT || item.isEditing()) {
                        item.setNewConditionStatus(Utils.getConditionStatus(angles));
                    }
                    setConditionStatusText(item);
                } else {
                    layoutResult.setVisibility(hasAngle ? View.VISIBLE : View.GONE);
                    item.setNewAngleText(Utils.getAngleText(item));
                    tvDiagnosisResult.setText(item.getNewAngleText());
                }
                if (TextUtils.isEmpty(item.getDescribe()) && (TextUtils.isEmpty(item.getNewDescribe()) || item.isNotEditDescribe())) {
                    item.setNewDescribe(Utils.getDescribe(item, baseActivity));
                }
                setDescribe(item);
            });
        }
        ivRightAvatar.setOnClickListener(v -> {
            if (item.getDoctorInfo() != null) {
                IntentUtils.goDoctorDetail(baseActivity, item.getDoctorInfo());
            }
        });
        layoutConditionStatus.setOnClickListener(v -> EditDescribeActivity.startForResult(baseActivity, item));
    }

    private void setConditionStatusText(ImageInfo item) {
        tvConditionStatusTitle.setText(Utils.getConditionStatusText(item.getNewConditionStatus(), baseActivity));
    }

    private void setDescribe(ImageInfo item) {
        tvConditionStatusDesc.setText(item.getNewDescribe());
    }
}
